﻿<%@ Application Language="C#" %>
<%@ Import Namespace = "System.Web.Security" %>
<%@ Import Namespace = "System.Security.Principal" %>

<script runat="server">

    void Application_Start(object sender, EventArgs e) 
    {
        // Code that runs on application startup

    }
    
    void Application_End(object sender, EventArgs e) 
    {
        //  Code that runs on application shutdown

    }
        
    void Application_Error(object sender, EventArgs e) 
    { 
        // Code that runs when an unhandled error occurs

    }

    void Session_Start(object sender, EventArgs e) 
    {
        // Code that runs when a new session is started

    }

    void Session_End(object sender, EventArgs e) 
    {
        // Code that runs when a session ends. 
        // Note: The Session_End event is raised only when the sessionstate mode
        // is set to InProc in the Web.config file. If session mode is set to StateServer 
        // or SQLServer, the event is not raised.

    }
    protected void Application_AuthenticateRequest(object sender, EventArgs e)
    {
        ////Recupero la cookie
        //HttpCookie authCookie = Context.Request.Cookies[FormsAuthentication.FormsCookieName];
        //if (null == authCookie)
        //{
        //    //Si no existe termina
        //    return;
        //}
        //FormsAuthenticationTicket autTicket = null;
        //try
        //{
        //    autTicket = FormsAuthentication.Decrypt(authCookie.Value);
        //}
        //catch (Exception ex)
        //{
        //    //No se pudo desencriptar
        //    return;
        //}
        ////Separo los roles
        //string[] roles = autTicket.UserData.Split(new char[] { '|' });
        ////Creo un objeto Identity y lo vinculo al Context
        //GenericIdentity id = new GenericIdentity(autTicket.Name, FormsAuthentication.FormsCookieName);
        //GenericPrincipal principal = new GenericPrincipal(id, roles);
        //Context.User = principal;
        //***********************************************************************
        //Otro forma
        //Se comprueba que el usuario este autenticado
        if (HttpContext.Current.User != null)
        {
            //Se comprueba si el usuario este autenticado
            if (HttpContext.Current.User.Identity.IsAuthenticated)
            {
                //Se comprueba si el usuario esta autenticado por formulario
                if (HttpContext.Current.User.Identity is FormsIdentity)
                {
                    //Se recupera la identidad del usuario para recuperar sus roles
                    FormsIdentity identity = (FormsIdentity)HttpContext.Current.User.Identity;
                    //Se recupera el ticket del usuario
                    FormsAuthenticationTicket ticket = identity.Ticket;
                    //Se recupera la informacion acerca del usuario, donde metimos la informacion de los roles
                    string[] roles = ticket.UserData.Split('|');
                    //Se crea un usuario con dichos roles
                    HttpContext.Current.User = new GenericPrincipal(identity, roles);
                }
            }
        }
    }
       
</script>
